package org.agony2d.gpu.core {
	import flash.geom.Matrix
	import flash.geom.Point;
	import flash.utils.ByteArray
	import org.agony2d.core.agony_internal;
	
	use namespace agony_internal;
	
final public class VertexCombine extends VertexProxy {
	
	agony_internal function setVertexCoords( width:int, height:int, matrix:Matrix ) : void {
		this.bytes.position = 0
		this.bytes.writeFloat(matrix.tx)
		this.bytes.writeFloat(matrix.ty)
		
		this.bytes.position = BYTES_TOTAL
		this.bytes.writeFloat(matrix.a * width + matrix.tx)
		this.bytes.writeFloat(matrix.b * width + matrix.ty)
		
		this.bytes.position = BYTES_TOTAL * 2
		this.bytes.writeFloat(matrix.c * height + matrix.tx)
		this.bytes.writeFloat(matrix.d * height + matrix.ty)
		
		this.bytes.position = BYTES_TOTAL * 3
		this.bytes.writeFloat(matrix.a * width  + matrix.c * height + matrix.tx)
		this.bytes.writeFloat(matrix.d * height + matrix.b * width  + matrix.ty)
	}
	
	agony_internal function setUV( left:Number, right:Number, top:Number, bottom:Number ) : void {
		this.bytes.position = BYTES_UV_OFFSET
		this.bytes.writeFloat(left)
		this.bytes.writeFloat(top)
		
		this.bytes.position = BYTES_TOTAL + BYTES_UV_OFFSET
		this.bytes.writeFloat(right)
		this.bytes.writeFloat(top)
		
		this.bytes.position = BYTES_TOTAL * 2 + BYTES_UV_OFFSET
		this.bytes.writeFloat(left)
		this.bytes.writeFloat(bottom)
		
		this.bytes.position = BYTES_TOTAL * 3 + BYTES_UV_OFFSET
		this.bytes.writeFloat(right)
		this.bytes.writeFloat(bottom)
	}
	
	agony_internal function setTint( color:uint ) : void {
		this.bytes.position = BYTES_COLOR_OFFSET
		this.bytes.writeUnsignedInt(color)
		
		this.bytes.position = BYTES_TOTAL * 1 + BYTES_COLOR_OFFSET
		this.bytes.writeUnsignedInt(color)
		
		this.bytes.position = BYTES_TOTAL * 2 + BYTES_COLOR_OFFSET
		this.bytes.writeUnsignedInt(color)
		
		this.bytes.position = BYTES_TOTAL * 3 + BYTES_COLOR_OFFSET
		this.bytes.writeUnsignedInt(color)
	}
	
	agony_internal function recycle() : void {
		cachedCombineList[cachedCombineLength++] = this
	}
	
	agony_internal static function NewVertexCombine() : VertexCombine {
		return (cachedCombineLength > 0 ? cachedCombineLength-- : 0) ? cachedCombineList.pop() : new VertexCombine
	}
	
	agony_internal static var cachedCombineList:Array = []
	agony_internal static var cachedCombineLength:int
}
}